package net.kleinhenz.droidshop.service;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import net.kleinhenz.droidshop.connector.Amazon;
import net.kleinhenz.droidshop.connector.Currencies;
import net.kleinhenz.droidshop.connector.ProductInfoResource;
import net.kleinhenz.droidshop.model.Locale;
import net.kleinhenz.droidshop.model.LogEntry;
import net.kleinhenz.droidshop.system.PersistenceConfiguration;
import net.kleinhenz.schemas.droidshop.Currency;
import net.kleinhenz.schemas.droidshop.CurrencyList;
import net.kleinhenz.schemas.droidshop.Message;
import net.kleinhenz.schemas.droidshop.MessageList;
import net.kleinhenz.schemas.droidshop.Motd;
import net.kleinhenz.schemas.droidshop.Product;
import net.kleinhenz.schemas.droidshop.ProductList;
import net.kleinhenz.schemas.droidshop.Wishlist;
import net.kleinhenz.schemas.droidshop.WishlistEntry;
import net.kleinhenz.schemas.droidshop.Wishlists;

@Path("/")
@Produces("application/xml")
public class DroidShopServicePortTypeImpl
{
	private static final Currencies currencies = new Currencies();
	
	@GET
    @Path("/isServerAlive")
	public Motd isServerAlive()
	{
		Motd motd = new Motd();
		motd.setMajor("1");
		motd.setMinor("0");
		motd.setMicro("0");
		MessageList messageList = new MessageList();
		motd.setMessages(messageList);
		CurrencyList currencyList = new CurrencyList();
		motd.setCurrencies(currencyList);
		
		Message message = new Message();
		message.setLocale(Locale.LOCALE_DE);
		message.setText("Willkommen zu DroidShop!");
		messageList.getMessage().add(message);

		message = new Message();
		message.setLocale(Locale.LOCALE_UK);
		message.setText("Welcome to DroidShop UK!");
		messageList.getMessage().add(message);
		
		message = new Message();
		message.setLocale(Locale.LOCALE_US);
		message.setText("Welcome to DroidShop US!");
		messageList.getMessage().add(message);
		
		Currency currency = new Currency();
		currency.setCurrencyname("USD");
		currency.setValue(currencies.euroInCurrency(Currencies.USD));
		currencyList.getCurrency().add(currency);
		
		currency = new Currency();
		currency.setCurrencyname("GBP");
		currency.setValue(currencies.euroInCurrency(Currencies.GBP));
		currencyList.getCurrency().add(currency);

		return motd;
	}

	@GET
    @Path("/search/{locale}/{client}/{ip}/{keywords}")
	public ProductList searchProducts(
			@PathParam("locale") String locale,
			@PathParam("client") String client,
			@PathParam("ip") String ipAddress,
			@PathParam("keywords") String keywords)
	{
		ProductInfoResource shop = new Amazon(); 
		ProductList result = shop.searchProducts(keywords, locale);

		EntityManager em = PersistenceConfiguration.getEntityManagerFactory().createEntityManager();
		EntityTransaction tx = em.getTransaction();
        tx.begin();
		LogEntry logEntry = new LogEntry(LogEntry.TYPE_SEARCH, ipAddress, client, locale, keywords, result.getProduct().size());
		em.persist(logEntry);
		tx.commit();
		em.close();

		return result;
	}

	@GET
    @Path("/lookup/{locale}/{client}/{ip}/{ean}")
	public Product lookupProducts(
			@PathParam("locale") String locale,
			@PathParam("client") String client,
			@PathParam("ip") String ipAddress,
			@PathParam("ean") String ean)
	{
		ProductInfoResource shop = new Amazon();
		Product result = shop.lookupProduct(ean, locale);

		EntityManager em = PersistenceConfiguration.getEntityManagerFactory().createEntityManager();
		EntityTransaction tx = em.getTransaction();
        tx.begin();
		LogEntry logEntry = new LogEntry(LogEntry.TYPE_LOOKUP, ipAddress, client, locale, ean, 1);
		em.persist(logEntry);
		tx.commit();
		em.close();

		return result;
	}
	
	@PUT
	@Path("/wishlist/{user}/{secret}/{title}")
	@Produces("text/plain")
	public String createWishlist(
			@PathParam("user") String user, 
			@PathParam("secret") String secret, 
			@PathParam("title") String title)
	{
		System.out.println("CREATE WISHLIST USER: " + user + " SECRET: " + secret + " TITLE: " + title);
		return "abc";
	}
	
	@DELETE
	@Path("/wishlist/{user}/{secret}/{id}")
	public void removeWishlistOrWishlistEntry(
			@PathParam("user") String user, 
			@PathParam("secret") String secret, 
			@PathParam("id") String id)
	{
		System.out.println("REMOVE WISHLIST USER: " + user + " SECRET: " + secret + " id: " + id);
	}

	@GET
	@Path("/wishlist/{user}/{secret}/{id}")
	public Wishlist getWishlist(
			@PathParam("user") String user, 
			@PathParam("secret") String secret, 
			@PathParam("id") String id)
	{
		System.out.println("GET WISHLIST USER: " + user + " SECRET: " + secret + " ID: " + id);
		return new Wishlist();
	}

	@POST
	@Path("/wishlist/{user}/{secret}/{id}")
	@Consumes("text/xml")
	public void addToWishlist(
			@PathParam("user") String user, 
			@PathParam("secret") String secret, 
			@PathParam("id") String id, 
			WishlistEntry entry)
	{		
		System.out.println("ADD WISHLIST USER: " + user + " SECRET: " + secret + " ID: " + id + " ENTRYID: " + entry.getId());
	}

	@GET
	@Path("/wishlists/{user}/{secret}")
	public Wishlists getWishlists(String user, String secret)
	{
		System.out.println("USER: " + user + " SECRET: " + secret);
		return new Wishlists();
	}	
}
